package com.itheima.leadnews.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {

    //死信交换机
    public static final String DEAD_LETTER_EXCHANGE_NAME = "dead.exchange";
    //死信队列
    public static final String DEAD_LETTER_QUEUE_NAME = "dead.queue";
    //死信路由规则
    public static final String DEAD_ROUTING_KEY = "dead";
    //消息队列
    public static final String SEND_QUEUE_NAME = "send.queue";

    /**
     * 创建死信交换机
     */
    @Bean
    public Exchange creatDeadLetterExchange() {
        return ExchangeBuilder.directExchange(DEAD_LETTER_EXCHANGE_NAME).durable(true).build();
    }

    /**
     * 创建死信队列
     */
    @Bean
    public Queue creatDeadLetterQueue() {
        return QueueBuilder.durable(DEAD_LETTER_QUEUE_NAME).build();
    }

    /**
     * 绑定死信交换机和队列
     */
    @Bean
    public Binding bindingDeadLetterExchange2Queue(@Qualifier("creatDeadLetterExchange") Exchange exchange,
                                                   @Qualifier("creatDeadLetterQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_ROUTING_KEY).noargs();
    }

    /**
     * 创建消息队列,指定死信交换机和路由规则
     */
    @Bean
    public Queue sendQueue(){
        return QueueBuilder.durable(SEND_QUEUE_NAME).deadLetterExchange(DEAD_LETTER_EXCHANGE_NAME).deadLetterRoutingKey(DEAD_ROUTING_KEY).build();
    }
}
